package com.xm.xmapp.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.xm.xmapp.R;
import com.xm.xmapp.tools.ScreenUtils;

public class InstructionTextView extends View {

	String text = "";
	Paint _paint = new Paint();
	Paint _paint_text = new Paint();

	int textHeight;
	int lineHeight;
	int line;
	int textleft;
	int textright;
	int texttop;
	int textbottom;
	int withSize;

	public InstructionTextView(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.customLayout);

		int textColor = a.getColor(R.styleable.customLayout_textColor,
				0XFFFFFFFF);
		float textSize = a.getDimension(R.styleable.customLayout_textSize, 32);

		text = (String) a.getText(R.styleable.customLayout_customtext);

		if (text == null) {
			text = " ";
		}

		if (textSize != 0) {
			_paint_text.setTextSize(textSize);
		}

		_paint_text.setColor(textColor);

		_paint_text.setTypeface(Typeface.DEFAULT);

		int bgColor = a.getColor(R.styleable.customLayout_bgColor, 0XFFFFFFFF);
		_paint.setColor(bgColor);

		initData(context);

		a.recycle();

	}

	private void initData(Context context) {
		// TODO Auto-generated method stub
		lineHeight = (int) getResources().getDimension(R.dimen.dp23);
		textleft = (int) getResources().getDimension(R.dimen.dp10);
		textright = (int) getResources().getDimension(R.dimen.dp10);
		texttop = (int) getResources().getDimension(R.dimen.dp5);
		textbottom = (int) getResources().getDimension(R.dimen.dp5);
		width = ScreenUtils.getScreenWidth(context) - getPaddingLeft()
				- getPaddingRight() - textleft - textright;

	}

	public void setText(String text) {
		this.text = text;
		invalidate();
	}

	int width;

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		// int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		int height;

		if (heightMode == MeasureSpec.EXACTLY) {
			height = heightSize;
		} else {

			textHeight = (int) -(_paint_text.ascent() + _paint_text.descent());

			lingWith = _paint_text.measureText("我");
			withSize = (int) (width / lingWith);
			line = text.length() / withSize;
			if ((withSize * line) != text.length())
				line++;

			int desired = (int) (textHeight + lineHeight + texttop + textbottom);
			height = desired * line + getPaddingTop() + getPaddingBottom()
					- lineHeight;
		}
		setMeasuredDimension(widthSize, height);

	}

	RectF r2 = new RectF();
	float lingWith;
	Rect mBounds = new Rect();

	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);

		r2.left = getPaddingLeft();
		r2.right = getWidth() - getPaddingRight();

		for (int i = 0; i < line; i++) {

			r2.top = (textHeight + lineHeight + texttop + textbottom) * i
					+ getPaddingTop();
			r2.bottom = textHeight + r2.top + texttop + textbottom
					+ getPaddingTop();

			String str = text
					.substring(
							i * withSize + (i == 0 ? 0 : 1),
							(i * withSize + (i == 0 ? 1 : 1) + withSize) <= text
									.length() ? (i * withSize
									+ (i == 0 ? 1 : 1) + withSize) : text
									.length());

			_paint_text.getTextBounds(str, 0, str.length(), mBounds);

			if (i == (line - 1))
				r2.right = r2.left + textleft + _paint_text.measureText(str)
						+ textright;

			canvas.drawRoundRect(r2, 10, 10, _paint);
			canvas.drawText(str, r2.left + textleft, r2.top + texttop
					+ textHeight + textbottom, _paint_text);

		}

	}

}
